var move = {
  infos: function (target) {
    try {
      var offset = target.offset();
      return {
        left: offset.left,
        right: offset.left + target.width(),
        up: offset.top,
        down: offset.top + target.height(),
      };
    } catch (error) {
      console.log("请用jq对象");
    }
  },
  rules: function (pinfo, ninfo, pDvalue, mDvalue, dir) {
    var tmp, pref, min;
    if (dir === "up") {
      if (pinfo.up > ninfo.down) {
        tmp = this.distance(ninfo.left, ninfo.down, pinfo.left, pinfo.up);
        (!mDvalue || tmp < mDvalue) && ((mDvalue = tmp), (min = true));
        this.contains(ninfo.left, ninfo.right, pinfo.left, pinfo.right) &&
          (!pDvalue || tmp < pDvalue) &&
          ((pDvalue = tmp), (pref = true));
      }
    } else if (dir === "down") {
      if (pinfo.down < ninfo.up) {
        tmp = this.distance(ninfo.left, ninfo.up, pinfo.left, pinfo.down);
        (!mDvalue || tmp < mDvalue) && ((mDvalue = tmp), (min = true));
        this.contains(ninfo.left, ninfo.right, pinfo.left, pinfo.right) &&
          (!pDvalue || tmp < pDvalue) &&
          ((pDvalue = tmp), (pref = true));
      }
    } else if (dir === "left") {
      if (pinfo.left > ninfo.right) {
        tmp = this.distance(ninfo.right, ninfo.up, pinfo.left, pinfo.up);
        (!mDvalue || tmp < mDvalue) && ((mDvalue = tmp), (min = true));
        this.contains(ninfo.up, ninfo.down, pinfo.up, pinfo.down) &&
          (!pDvalue || tmp < pDvalue) &&
          ((pDvalue = tmp), (pref = true));
      }
    } else if (dir === "right") {
      if (pinfo.right < ninfo.left) {
        tmp = this.distance(ninfo.left, ninfo.up, pinfo.right, pinfo.up);
        (!mDvalue || tmp < mDvalue) && ((mDvalue = tmp), (min = true));
        this.contains(ninfo.up, ninfo.down, pinfo.up, pinfo.down) &&
          (!pDvalue || tmp < pDvalue) &&
          ((pDvalue = tmp), (pref = true));
      }
    }
    return {
      pDvalue: pDvalue,
      mDvalue: mDvalue,
      pref: pref,
      min: min,
    };
  },
  next: function (dir) {
    var pointer = $(".focus"),
      pinfo = this.infos(pointer);
    var ninfo, pDvalue, mDvalue, pref, min;
    var sets = $(".item");
    for (var i = 0, len = sets.length; i < len; i++) {
      var $el = $(sets[i]);
      if (pointer.attr("id") != $el.attr("id")) {
        ninfo = this.infos($el);
        var rule = this.rules(pinfo, ninfo, pDvalue, mDvalue, dir);
        pDvalue = rule.pDvalue;
        mDvalue = rule.mDvalue;
        rule.pref && (pref = sets[i]);
        rule.min && (min = sets[i]);
      }
    }
    if (dir == "left" || dir == "right") {
      return pref
    } else if (dir == "up" || dir == "down") {
      return pref || min;
    }
  },
  distance: function (cx, cy, nx, ny) {
    return parseInt(Math.sqrt(Math.pow(cx - nx, 2) + Math.pow(cy - ny, 2)));
  },
  contains: function (cmin, cmax, nmin, nmax) {
    return (
      cmax - cmin + (nmax - nmin) >=
      Math.max(cmin, cmax, nmin, nmax) - Math.min(cmin, cmax, nmin, nmax)
    );
  },
  up: function () {
    return this.next("up");
  },
  down: function () {
    return this.next("down");
  },
  left: function () {
    return this.next("left");
  },
  right: function () {
    return this.next("right");
  },
};
